/**
* Created caobaolin. 
* Author: caobaolin
* Date: 6/13/12 
* PhoneGAP plugin pushtoken 
 test copy 1.0.2
*/

var webView = function () {};


webView.prototype.sendAsynchronousRequest = function(url,param,callback) {
    var _callback = function(args) {
        callback(args);
    };
    var config = {
        url : url + '' || '',
        param : param
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'sendAsynchronousRequest', [config]);
};

webView.prototype.getSystemConfig = function(jscallback) {
    var _callback = function(args) {
        jscallback(args);
    };
    
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getSystemConfig', []);
};

/**
 * js 中的调用方法，打开新的界面
 *
 * @param url
 *            请求页面地址
 * @param titletype
 *            新页面标题类型
 *
 * @param titletext
 *            标题名
 */

webView.prototype.openWebView = function(url, titletype, titletext) {
    
    var config = {
        index_url : url + '' || '',
        title : titletext+'' || '',
        titletype : titletype
    };
    
    var _callback = function(args) {
        //success(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'openWebView', [config]);
}

webView.prototype.backToRootView = function()
{
	var _callback = function(args) {
        //jscallback(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'backToRootView',[]);
}

/**
 * 判断是否有网络连接，无网络则提示
 */
webView.prototype.goBack = function() {
    var _callback = function(args) {
        //        var result = Boolean(args)
        //        jscallback(result);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'goBack', []);
};


/**
 * js 中的调用方法，设置界面中native的title标签
 *
 * @param title
 *            标题名
 */
webView.prototype.setTitleTxt = function(title) {
    var _callback = function(args) {
        //jscallback(args);
    };
    
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'setTitleTxt', [title]);
}


/**
 * 提示信息
 *
 * @param message
 *            提示的信息
 */

webView.prototype.showToast = function(message) {
    var _callback = function(args) {
    };
    message = message.toString();
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'showToast', [message]);
};

/**
 * boolen == true 显示loading层.
 * boolen == false 去除loading层.
 */
webView.prototype.setProgressBarVisibility = function(boolen) {
    if(!boolen)
    {
        boolen = false;
    }
    var _callback = function(args) {
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'setProgressBarVisibility', [boolen]);
    
};

/**
 * boolen == true 显示loading层.
 * boolen == false 去除loading层.
 */
webView.prototype.showLoading = function(boolen) {
    if(!boolen)
    {
        boolen = false;
    }
    var _callback = function(args) {
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'showLoading', [boolen]);
    
};

/**
 * js 中的调用方法，通过native处理网络请求数据，再将数据返回给js 参数 jscallback 回调函数 requestType 请求类型
 *
 * @param url
 *            访问URL
 * @param jscallback
 *            回调函数，并传递字符串给用户
 * @param requestType
 *            请求方式 'get', 'post'
 */
webView.prototype.getJsonByGet = function(jscallback, url, requestType) {
    var config = {
        index_url : url + '' || '',
        requestType : requestType + '' || '',
    };
    var _callback = function(args) {
        jscallback(args);
    };
    
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getJsonByGet', [config]);
}

/**
 
 */

webView.prototype.toSysBrowser = function(url) {
	var config = {
        url : url
    };
	
	var _callback = function(args) {
    	
    };
    
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'toSysBrowser', [config]);
}


/**
 * 截取url中“?”之后的字符串.
 *
 * @param jscallback
 *            回调函数，并传递字符串给用户
 */
webView.prototype.getCallbackParams = function(jscallback) {
    var _callback = function(args) {
        jscallback(args);
        return args;
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getCallbackParams', []);
}

webView.prototype.callSystemAbility = function(systemAction) {
    
    var config = {
        systemAction : systemAction
    };
    
    var _callback = function(args) {
        //success(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'callSystemAbility', [config]);
}

webView.prototype.getUserName = function(jscallback) {
    var _callback = function(args) {
        jscallback(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getUserName', []);
}
webView.prototype.getPassword = function(jscallback) {
    var _callback = function(args) {
        jscallback(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getPassword', []);
}

/**
 *  加密的用户名和密码
 */

webView.prototype.getDesUserName = function(jscallback) {
    var _callback = function(args) {
        jscallback(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getDesUserName', []);
}

webView.prototype.getDesPassword = function(jscallback) {
    var _callback = function(args) {
        jscallback(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getDesPassword', []);
}


/**
 * js 中的调用方法，预览图片
 *
 * @param index
 *            图片序号（0,1,2,3）
 * @param images
 *            图片URL
 * @param names
 *            图片名
 * @param xs
 *            ys 经纬度
 */

webView.prototype.showImage = function(index, imageStr, nameStr, xStr, yStr) {
    var arr = new Array();
    var images = new Array();
    var names = new Array();
    var xs = new Array();
    var ys = new Array();
    
    
    images = imageStr.split(",");
    names = nameStr.split(",");
    xs = xStr.split(",");
    ys = yStr.split(",");
    
    var count = 0;
    
    if (isArray(images))
    {
        count = images.length;
    }
    
    for(var i = 0; i < count; i++)
    {
        
        var image = images[i];
        var name = "";
        
        if (isArray(names) && names.length>i)
        {
            name = names[i];
        }
        
        if (isArray(xs) && xs.length>i)
        {
            var x = xs[i];
        }
        
        if (isArray(ys) && ys.length>i)
        {
            var y = ys[i];
        }
        
        var config = {
            image : image,
            name : name,
            latitude : x,
            longitude : y
        };
        
        arr.push(config);
        
    }
    
    var _callback = function(args) {
        
    };
    
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'showImage', [index, arr]);
};
function isArray(object){
    return object && typeof object==='object' && Array == object.constructor;
}


/**
 * js 中的调用方法，设置键值对
 *
 * @param key
 *            键名
 * @param value
 *            值
 *
 */
webView.prototype.setKeyAndValue = function(key, value) {
    var config = {
        key : key + '' || '',
        value : value || '',
    };
    var _callback = function(args) {
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'setKeyAndValue', [config]);
};



/**
 * 根据键名获取值
 *
 * @param jscallback
 *            回调函数，并传递字符串给用户
 * @param key
 *            键名
 */
webView.prototype.getValueByKey = function(jscallback,key) {
    //    alert(key);
    var _callback = function(args) {
        //        alert(args);
        jscallback(args);
    };
    
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getValueByKey', [key]);
}

/**
 * 获取当前经纬度
 *
 * @param jscallback
 *            回调函数，并传经纬度给用户
 *
 */
webView.prototype.getXY = function(jscallback,key) {
    var _callback = function(args) {
        var x = parseFloat(args.x);
        var y = parseFloat(args.y);
        jscallback(x, y);
    };
    
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getLocation', [key]);
}
webView.prototype.getCityName = function(jscallback) {
    
    var _callback = function(args) {
        jscallback(args);
    };
    
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getCityName', []);
}

/**
 * 拍照并返回图片路径
 *
 * @param jscallback
 *            回调函数，并传递字符串给用户
 * 
 */
webView.prototype.callSysCamera = function(jscallback,flag) {

    navigator.camera.getPicture(onPhotoDataSuccess, function(ex) { 
                                // alert("Camera Error!"); 
                                }, { 
                                quality : 50, 
                                targetWidth: 240, 
                                targetHeight: 240, 
                                encodingType: Camera.EncodingType.JPEG,
                                //用data_url,而不用file_url的原因是,file_url在不同平台有差异 
                                destinationType:Camera.DestinationType.FILE_URI,
                                sourceType:Camera.PictureSourceType.CAMERA
                                });
    function onPhotoDataSuccess(imageData) {
        jscallback(imageData);    
    }
    
}

/**
 * 从图片库选取图片并返回路径
 *
 * @param jscallback
 *            回调函数，并传递字符串给用户
 * 
 */
webView.prototype.callSysAlbum = function(jscallback) {

    navigator.camera.getPicture(onPhotoDataSuccess, function(ex) { 
                                 //alert("Camera Error!"); 
                                }, { 
                                quality : 50, 
                                targetWidth: 240, 
                                targetHeight: 240, 
                                encodingType: Camera.EncodingType.JPEG,
                                //用data_url,而不用file_url的原因是,file_url在不同平台有差异 
                                destinationType:Camera.DestinationType.FILE_URI,
                                sourceType:Camera.PictureSourceType.PHOTOLIBRARY
                                });

    function onPhotoDataSuccess(imageData) {
        jscallback(imageData);    
    }
}

//获取当前应用内语言设置接口
//返回结果：0中文 1英文
webView.prototype.getCurrLan = function(jscallback){
    var _callback = function(args) {
        jscallback(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getCurrLan', []);
}


//学工二维码
webView.prototype.openQRCoderYX = function(jscallback) {
    var _callback = function(args) {
        jscallback(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'openQRCoderYX', []);
}
/*
 from to 是起始位置到结束位置坐标点的字符串
 latitude:longitude  纬度在前，经度在后，冒号隔开
 */
webView.prototype.mapNavigation = function(from,to){

    var arr = new Array();
    var fromarr = new Array();
    
    fromarr = from.split(":");
    var config1 = {
        latitude : fromarr[0],
        longitude : fromarr[1]
    };
    arr.push(config1);
    
    var toarr = new Array();
    toarr = to.split(":");
    
    var config2 = {
        latitude : toarr[0],
        longitude : toarr[1]
    };
    arr.push(config2);
    
    var _callback = function(args) {
        
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'mapNavigation', [arr]);
}
/*
 from to 是起始位置到结束位置坐标点的字符串
 latitude:longitude  纬度在前，经度在后，冒号隔开
 */
webView.prototype.naviNavigation = function(from,to){
    
    var arr = new Array();
    var fromarr = new Array();
    
    fromarr = from.split(":");
    var config1 = {
        latitude : fromarr[0],
        longitude : fromarr[1]
    };
    arr.push(config1);
    
    var toarr = new Array();
    toarr = to.split(":");
    
    var config2 = {
        latitude : toarr[0],
        longitude : toarr[1]
    };
    arr.push(config2);
    
    var _callback = function(args) {
        
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'naviNavigation', [arr]);
}
/*
 locationArr字符串数组
 latitude:longitude  纬度在前，经度在后，冒号隔开
 */
webView.prototype.mapShowAnnotations = function(locationArr){
    
    var arr = new Array();
    
    var count = 0;
    if(isArray(locationArr))
    {
        count = locationArr.length;
    }
    for(var i = 0 ; i < count; i++)
    {
        var tmparr = locationArr[i].split(":");
        var config = {
            latitude : tmparr[0],
            longitude : tmparr[1],
            title : tmparr[2]
        };
        arr.push(config);
    }
    var _callback = function(args) {
        
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'mapShowAnnotations', [arr]);
}

webView.prototype.openAttachment = function(url, name, type) {
    
    var config = {
        url : url + '' || '',
        name : name+'' || '',
        type : type
    };
    
    var _callback = function(args) {
        //success(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'openAttachment', [config]);
}

webView.prototype.refreshPrevPage = function() {
    var _callback = function(args) {
        
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'refreshPrevPage', []);
}
webView.prototype.getPushUrl = function(jscallback) {
    var _callback = function(args) {
        jscallback(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getPushUrl', []);
}
webView.prototype.getSignUser = function(jscallback) {
    var _callback = function(args) {
        jscallback(args);
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'getSignUser', []);
}
webView.prototype.setBackClose = function() {
    var _callback = function(args) {
        
    };
    return cordova.exec(
                        _callback,
                        _callback,
                        'com.wisedu.CDVWebView', 'setBackClose', []);
}